﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.ComponentModel;

namespace DAO
{
    public class UsersGroupDAO
    {
        public UsersGroupDTO GetUsersGroupByID(int userGroupID)
        {
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                LoaiNhanVien type = ent.LoaiNhanViens.FirstOrDefault(t => t.LoaiNhanVienID == userGroupID && t.LoaiNhanVienID != null);

                UsersGroupDTO userGroup = new UsersGroupDTO
                {
                    UsersGroupID = type.LoaiNhanVienID,
                    UsersGroupCode = type.MaLoaiNhanVien,
                    UsersGroupName = type.TenLoaiNhanVien,
                    Status = bool.Parse( type.SuDung.ToString())
                };

                return userGroup;
            }
        }

        public UsersGroupDTO CreateUsersGroupDTOFromEntity(LoaiNhanVien loaiNhanVien)
        {
            UsersGroupDTO UserDto = new UsersGroupDTO
            {
                UsersGroupCode = loaiNhanVien.MaLoaiNhanVien,
                UsersGroupID = loaiNhanVien.LoaiNhanVienID,
                UsersGroupName = loaiNhanVien.TenLoaiNhanVien,
                Status = bool.Parse(loaiNhanVien.SuDung.ToString())
            };
            return UserDto;
        }

        public int InsertUsersGroup(UsersGroupDTO userGroup)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    LoaiNhanVien type = new LoaiNhanVien
                    {
                        MaLoaiNhanVien = userGroup.UsersGroupCode,
                        TenLoaiNhanVien = userGroup.UsersGroupName,
                        SuDung = userGroup.Status
                    };

                    if (ent.LoaiNhanViens.Count(t => t.LoaiNhanVienID != null) > 0)
                    {
                        type.LoaiNhanVienID = ent.LoaiNhanViens.Max(t => t.LoaiNhanVienID) + 1;
                    }
                    else
                    {
                        type.LoaiNhanVienID = 1;
                    }

                    ent.AddToLoaiNhanViens(type);

                    ent.SaveChanges();
                    return type.LoaiNhanVienID;
                }
            }
            catch (Exception ex)
            {

                return -1;
            }
        }

        public List<UsersGroupDTO> GetUsersGroupList()
        {
            List<UsersGroupDTO> listUsersGroupDto = new List<UsersGroupDTO>();
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                List<LoaiNhanVien> listLoaiNhanVien = ent.LoaiNhanViens.Where(c => c.SuDung == true).ToList();
                foreach (LoaiNhanVien item in listLoaiNhanVien)
                {
                    UsersGroupDTO UserDto = CreateUsersGroupDTOFromEntity(item);
                    listUsersGroupDto.Add(UserDto);
                }
            }
            return listUsersGroupDto;
        }

        public BindingList<UsersGroupDTO> GetNewBindingListUsersGroup()
        {
            BindingList<UsersGroupDTO> listUsersGroupDto = new BindingList<UsersGroupDTO>();
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                List<LoaiNhanVien> listLoaiNhanVien = ent.LoaiNhanViens.Where(c => c.SuDung == true).ToList();
                foreach (LoaiNhanVien item in listLoaiNhanVien)
                {
                    UsersGroupDTO UserDto = CreateUsersGroupDTOFromEntity(item);
                    listUsersGroupDto.Add(UserDto);
                }
            }
            return listUsersGroupDto;
        }

        public bool UpdateUsersGroup(UsersGroupDTO userDTO)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    LoaiNhanVien userEnt = ent.LoaiNhanViens.FirstOrDefault(c => c.LoaiNhanVienID == userDTO.UsersGroupID);

                    userEnt.TenLoaiNhanVien = userDTO.UsersGroupName;

                    ent.SaveChanges();

                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public UsersGroupDTO GetCategoryByID(int userID)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    LoaiNhanVien cate = ent.LoaiNhanViens.FirstOrDefault(c => c.LoaiNhanVienID == userID);

                    UsersGroupDTO userDTO = CreateUsersGroupDTOFromEntity(cate);

                    return userDTO;
                }
            }
            catch
            {
                return null;
            }
        }

        public bool DeleteUsersGroup(int userID)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    LoaiNhanVien userEnt = ent.LoaiNhanViens.FirstOrDefault(c => c.LoaiNhanVienID == userID);
                    userEnt.SuDung = false;
                    ent.SaveChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public bool CheckExistsCode(string code)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    int count = ent.LoaiNhanViens.Where(c => c.MaLoaiNhanVien == code).Count();

                    if (count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch
            {
                return true;
            }
        }
    }
}
